package com.vm.myview;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.vm.commons.ProgressUtil;
import com.vm.commons.StringUtil;
import com.vm.db.DownInfoDao;
import com.vm.model.ActiveSoft;
import com.vm.model.DownInfo;
import com.vm.model.Soft;
import com.vm.utils.ApkUtils;
import com.vm.utils.DownloadThread;
import com.wwt.app.R;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月28日 下午2:18:14]
 * @Version: [v1.0]
 * 
 */
public class DownloadProgress {

	private Context mContext;
	LinearLayout layout;
	Button btn_download;
	ProgressBar progressBar;
	RelativeLayout rlayout_info;
	RelativeLayout rlayout_progressBar;
	View view;

	DownInfoDao dao;
	DownInfo apkInfo;
	ActiveSoft activeSoft;

	public DownloadProgress(Context context, View view, ActiveSoft activeSoft) {

		this.mContext = context;
		this.view = view;

		dao = new DownInfoDao(context);
		this.activeSoft = activeSoft;
		initProgressBar();
	}

	public void initProgressBar() {

		init();
		File apkfile = new File(StringUtil.getSdApkUrl(activeSoft.getName()));
		if (apkfile.exists()) {

			// 判断APK在机器的状态,正在下载,已下载,已安装
			apkInfo = dao.query(activeSoft.getSoftId());
			if (apkInfo != null) {
				progressBar.setMax(apkInfo.getApkLength());
				if (apkInfo.getDone() <= apkInfo.getApkLength()) {

					if (apkInfo.getIsPause() == 0) {
						// 下载APK
						progressBar.setProgress(ProgressUtil.hashMap
								.get(apkInfo.getSoftId()));

						btn_download.setText("暂停");
					} else {
						btn_download.setText("继续");
					}

				}
			} else {
				btn_download.setText("安装");
			}

		}
	}

	/**
	 * 初始化控件
	 */
	private void init() {

		btn_download = (Button) view.findViewById(R.id.btn_download);
		btn_download.setOnClickListener(downClickListener);

		progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
		rlayout_info = (RelativeLayout) view.findViewById(R.id.rlayout_info);
		rlayout_progressBar = (RelativeLayout) view
				.findViewById(R.id.rlayout_progressBar);

	}

	/**
	 * 下载按钮监听
	 */
	public OnClickListener downClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			// 执行安装
			Toast toast3 = Toast.makeText(mContext, "正在安装", 500);
			toast3.setGravity(Gravity.CENTER, 0, 0);
			toast3.show();

			File apkfile = new File(
					StringUtil.getSdApkUrl(activeSoft.getName()));
			if (!apkfile.exists()) {
				return;
			}
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
					"application/vnd.android.package-archive");
			view.getContext().startActivity(i);

		}
	};

}
